package com.team7.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team7.order.dao.PlayHistoryDao;
import com.team7.order.entity.PlayHistory;
import com.team7.order.service.PlayHistoryService;
import com.team7.order.utils.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张煜杰
 * @since 2021-10-28
 */
@RestController
@RequestMapping("/order/play-history")
public class PlayHistoryController {
    @Resource
    private PlayHistoryService playHistoryService;
    @Resource
    private PlayHistoryDao playHistoryDao;
    //插入一条播放历史信息表
    @PostMapping("/insertPlayHistoryByCourseId/{courseID}/{userId}/{vedeo_id}")
    @ApiOperation(value = "插入或者更新消息(tb_play_history)")
    public CommonResult insertPlayHistoryByCourseId(@PathVariable("courseID") String courseID, @PathVariable("userId") String userId,@PathVariable("vedeo_id") String vedeoId){
        QueryWrapper<PlayHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("vedeo_id",vedeoId);
        PlayHistory selectOne = playHistoryDao.selectOne(wrapper);
    //System.out.println("查询的数据"+selectOne);
        CommonResult commonResult=null;
        if (selectOne==null){
    //插入
            commonResult = playHistoryService.insertPlayHistoryByCourseId(vedeoId, userId);
            commonResult=new CommonResult(2000,"插入成功",commonResult);

        }else{
    //更新
            commonResult = playHistoryService.updatePlayHistory(userId,vedeoId);
            commonResult=new CommonResult(2000,"更新成功",commonResult);

        }

        return commonResult;
    }
    //查询所有的播放信息
    @PostMapping("/queryPlayHistoryBy/{page}/{limit}/{HuiYuanId}")
    @ApiOperation(value = "查询tb_play_history")
    public CommonResult queryPlayHistoryBy(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, @PathVariable("HuiYuanId") String HuiYuanId, @RequestBody PlayHistory playHistory){
        CommonResult commonResult = playHistoryService.queryPlayHistory(page,limit,HuiYuanId,playHistory);
        return commonResult;
    }
    @PostMapping("/updatePlayHistory/{userId}/{vedeoId}")
    public CommonResult updatePlayHistory( @PathVariable("userId") String userId,@PathVariable("vedeo_id") String vedeoId){
    //查询
        CommonResult commonResult = playHistoryService.updatePlayHistory( userId,vedeoId);
        return commonResult;
    }


}

